package edu.zut.hb.software.teacher.controller;

import com.alibaba.fastjson.JSON;
import edu.zut.hb.software.base.utils.R;
import edu.zut.hb.software.base.web.spring.controller.GenericTreeController;
import edu.zut.hb.software.teacher.domain.TeacherGroup;
import edu.zut.hb.software.teacher.service.TeacherGroupService;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("teacherGroup")
public class TeacherGroupController extends GenericTreeController<TeacherGroup, Long, TeacherGroupService> {

    public static final Logger logger = LogManager.getLogger(TeacherGroupController.class.getName());

    TeacherGroupService teacherGroupService;

    @Autowired
    public void setTeacherGroupService(TeacherGroupService teacherGroupService) {
        this.teacherGroupService = teacherGroupService;
        this.manager=this.teacherGroupService;
        this.treeManager=this.teacherGroupService;
    }
    @ResponseBody
    @RequestMapping(value="/findAll/{page}/{pageSize}",method = {RequestMethod.GET,RequestMethod.POST})
    public R findAllTeacherGroup(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize){
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of(page,pageSize, sort);

        Page<TeacherGroup> list = teacherGroupService.findAll(pageable);

        HashMap<String,Object> map = new HashMap<>();
        map.put("number",list.getTotalElements());
        map.put("userList",list.getContent());
        logger.info("----------->"+list.getContent());
        //map集合转化为JSON字符串进行数据传输
//        String res = JSON.toJSONString(map);
        return R.ok().data(map);
    }

}
